#include "wmic.h"
#include <QString>
#include"QProcess"
#include "QDebug"
WMIC::WMIC()
{

}
QString WMIC::getWMIC(const QString &cmd)
{
    //获取cpu名称：wmic cpu get Name
    //获取cpu核心数：wmic cpu get NumberOfCores
    //获取cpu线程数：wmic cpu get NumberOfLogicalProcessors
    //查询cpu序列号：wmic cpu get processorid
    //查询主板序列号：wmic baseboard get serialnumber
    //查询BIOS序列号：wmic bios get serialnumber
    //查看硬盘：wmic diskdrive get serialnumber
    QProcess p;
    p.start("cmd",QStringList()<<"/c"<<cmd);//qt6需要用此命令
    p.waitForFinished(-1);
    QString result = QString::fromLocal8Bit(p.readAllStandardOutput());
    QStringList list = cmd.split(" ");
    result = result.remove(list.last(), Qt::CaseInsensitive);
    result = result.replace("\r", "");
    result = result.replace("\n", "");
    result = result.simplified();
    return result;
}

QString WMIC::getCpuName()
{
    return getWMIC("wmic cpu get name");
}

QString WMIC::getCpuId()
{
    return getWMIC("wmic cpu get processorid");
}

QString WMIC::getDiskNum()
{
    return getWMIC("wmic diskdrive where index=0 get serialnumber");
}
//生成注册码*********************************************************************************************************************
QString WMIC::IDNumbertoMachineCode(QString ID)//序列号到机器码的产生过程
{
    QByteArray bytes1=ID.toLatin1();//将生成的8位随机数存入数组 QString转换成QByteArray类型
    QString MachineCode="";
    for(int i=0;i<bytes1.size();i++){
        char b1=bytes1[i];
        QString s = QString("%1").arg(QString::number(b1,2),sizeof(b1)*8,'0');//'0'表示高位用0去占位，2表示2进制。
        QString s0=s.mid(0,1);
        QString s1=s.mid(1,1);
        QString s2=s.mid(2,1);
        QString s3=s.mid(3,1);
        QString s4=s.mid(4,1);
        QString s5=s.mid(5,1);
        QString s6=s.mid(6,1);
        QString s7=s.mid(7,1);
        QString s_new=s7+s5+s3+s1+s0+s2+s4+s6;
        bool ok;
        int c = s_new.toInt(&ok,2);//先转为10进制
        QString s_out = QString("%1").arg(c,2,16,QLatin1Char('0')); //10进制转16进制
        MachineCode.append(s_out);
    }
  return MachineCode;
}

QString WMIC::MachineCodetoIDNumber(QString MachineCode)//机器码到序列号的产生过程
{
    int len=MachineCode.size();
    QString ID="";
    for(int i=0;i<len/2;i++){
        QString str=MachineCode.mid(i*2,2);
        bool OK;
        QString str1;
        //16进制转化
        int val=str.toInt(&OK,16);
        //转换为二进制
        str1=str1.setNum(val,2);
        //8代表一共有八位，'0'代表用0填充
        QString s=QString("%1").arg(str1,8,QLatin1Char('0'));
        QString s0=s.mid(0,1);
        QString s1=s.mid(1,1);
        QString s2=s.mid(2,1);
        QString s3=s.mid(3,1);
        QString s4=s.mid(4,1);
        QString s5=s.mid(5,1);
        QString s6=s.mid(6,1);
        QString s7=s.mid(7,1);
        QString s_new=s4+s3+s5+s2+s6+s1+s7+s0;
        bool ok;
        int c = s_new.toInt(&ok,2);//先转为10进制
        char st = c;               //十进制数用char类型表示--->必须有此步直接转化不可行
        QString str_ascii = QString(st);
        ID.append(str_ascii);
    }
  return ID;
}
